thin.imports('/public/configure/conf.msg.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	layer.load(0, {
		shade: [0.08, '#000']
	});

	// 讀取消息列表
	factory.request('_msg_volist');

	$(document).on('click', '.btn-cmds', function(e) {
		var that = $(this),
			cmd = that.attr('data-cmd'),
			_id = that.attr('data-id'),
			proid = that.attr('data-proid');

		layer.load(0, {
			shade: [0.08, '#000']
		});

		if(cmd == 'see') {
			// 查看
			var isread = that.attr('data-isread'),
				ischat = that.attr('data-ischat');
			if(isread == 0) {
				__getdata_msgread['id'] = _id;
				factory.request('_msg_read', function() {
					//window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&isPull=1#details';
					//window.location.href = '/project/' + proid;
					window.location.href = '/project/' + proid + '.html?msgid=' + _id + '&isPull=1';
				});
			} else {
				//window.location.href = '/mobile/chain/project?proid=' + proid + '&msgid=' + _id + '&ischat=true&isPull=1#details';
				//window.location.href = '/project/' + proid;
				window.location.href = '/project/' + proid + '.html?msgid=' + _id + '&ischat=' + ischat + '&isPull=1&ischat=true';
			}
		} else if(cmd == 'interview') {
			// 約談
			__getdata_msgchat['id'] = _id;
			factory.request('_msg_chat');
		} else if(cmd == 'refuse') {
			// 拒絕
			__getdata_msgrefuse['id'] = _id;
			factory.request('_msg_refuse', _id);
		} else if(cmd == 'withdraw') {
			// 撤回
			__getdata_msgundo['id'] = _id;
			factory.request('_msg_undo', _id);
		}
	});
});

var __getdata_msgread = {},
	__getdata_msgchat = {},
	__getdata_msgrefuse = {},
	__getdata_msgundo = {};

// 讀取消息列表回調
function __complete_msgvolist(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			createMsgList(res.data.list);
			$('.nothing-box').hide();
		} else {
			// 暫無内容
			$('#msvolist-view').html('').hide();
			$('.nothing-box').show();
			//$('#msvolist-view').html('');
		}
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 創建消息列表
function createMsgList(data) {
	var ele = '';
	var _role = tool.__cookie.get('memberRole');
	for(var i in data) {
		var list = data[i];
		if(i % 2 == 1) {
			ele += '<div class="msg-box m-r-0x" data-id="' + list.id + '">';
		} else {
			ele += '<div class="msg-box" data-id="' + list.id + '">';
		}
		ele += '<span class="ms-cover">';
		ele += '<img src="' + list.project.logo + '" class="pic-circle"/>';
		ele += '<p class="m-y-3x text-center fs18r">' + list.project.title + '</p>';
		if(list.is_read == 0) {
			// dcdcdc (未查阅)
			ele += '<p class="status-area" style="color:#dcdcdc;">未查阅</p>';
		} else if(list.is_chat == 0) {
			// e64340 (未约谈)
			ele += '<p class="status-area" style="color:#e64340;">未约谈</p>';
		} else {
			// 1aad19 (已约谈)
			ele += '<p class="status-area" style="color:#1aad19;">已约谈</p>';
		}
		ele += '</span>';
		ele += '<div class="p-x-2x p-y-2x">';
		ele += '<div class="clearfix lh48x">';
		if(_role >= 2) {
			// 投資者
			ele += '<img class="pic-circle float-left" src="' + list.from_member_headimgurl + '" />';
			ele += '<p class="w180x ellipsis float-left m-l-1x">' + list.from_member_nickname + '</p>';
		} else {
			// 創業者
			ele += '<img class="pic-circle float-left" src="' + list.to_member_headimgurl + '" />';
			ele += '<p class="w180x ellipsis float-left m-l-1x">' + list.to_member_nickname + '</p>';
		}
		ele += '<p class="w140x float-right text-center">';
		if(list.is_chat == 1) {
			if(_role >= 2) {
				// 投資者電話
				ele += '<i class="icon-phone fs24r m-r-1x v-middle"></i>' + list.from_mobile;
			} else {
				// 創業者電話
				// ele += '<i class="icon-phone fs24r m-r-1x v-middle"></i>' + list.to_mobile;
			}
		} else {
			ele += '    ';
		}
		ele += '</p>';
		ele += '</div>';
		ele += '<div class="clearfix lh48x">';
		ele += '<p class="float-left">日期 ' + tool.formatDate('yy/mm/dd', list.create_time) + '</p>';
		ele += '<div class="w140x float-right text-center btn-cmd-box">';
		ele += '<a href="javascript:;" class="btn-cmd">操作<i class="icon-setting  m-l-1x"></i></a>';
		ele += '<ul>';
		if(_role >= 2) {
			// 投資者
			ele += '<li>';
			ele += '<a href="javascript:;" class="btn-cmds" data-cmd="see" data-id="' + list.id + '" data-proid="' + list.project_id + '" data-isread="' + list.is_read + '"data-ischat="' + list.is_chat + '">查看</a>';
			ele += '</li>';
			if(list.is_chat == 0) {
				ele += '<li>';
				ele += '<a href="javascript:;" class="btn-cmds" data-cmd="interview" data-id="' + list.id + '" data-proid="' + list.project_id + '">约谈</a>';
				ele += '</li>';
			}
			ele += '<li>';
			ele += '<a href="javascript:;" class="btn-cmds" data-cmd="refuse" data-id="' + list.id + '" data-proid="' + list.project_id + '">拒绝</a>';
			ele += '</li>';
		} else {
			// 創業者
			ele += '<li>';
			ele += '<a href="/project/' + list.project_id + '.html" class="btn-cmd-r">查看</a>';
			ele += '</li>';
			ele += '<li>';
			ele += '<a href="javascript:;" class="btn-cmds" data-cmd="withdraw" data-id="' + list.id + '" data-proid="' + list.project_id + '">撤回</a>';
			ele += '</li>';
		}
		ele += '</ul>';
		ele += '</div>';
		ele += '</div>';
		ele += '</div>';
		ele += '</div>';
	}
	$('#msvolist-view').html(ele).show();
}

// 約談查看回調
function __complete_msgread(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 201) {
		unkn();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 項目約談回調
function __complete_msgchat(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		window.location.reload();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 拒絕約談回調
function __complete_msgrefuse(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 204) {
		$('.msg-box[data-id=' + unkn + ']').remove();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 約談撤回回調
function __complete_msgundo(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 204) {
		$('.msg-box[data-id=' + unkn + ']').remove();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}